package com.nf.health.app.activity.my;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.activity.my.impl.MyContact;
import com.nf.health.app.http.Apis;
import com.nf.health.app.utils.NetUtil;
import com.nf.health.app.utils.ViewUtil;
import com.nf.health.app.utils.WebViewUtils;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 健康明白人健康管理方案
 */

public class HouseHealthyManagerProActivity extends BaseActivity {

    @Bind(R.id.titlebar_right_view)
    ImageView titlebar_right_view ;
    @Bind(R.id.wb_load_hourse_webview)
    WebView wb_load_hourse_webview ;
    String url = "";
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_househealthymanager_webview);
        ButterKnife.bind(this);
        ViewUtil.setText(this, R.id.titlebar_title_tv, "健康明白人知识大赛");
        regisitListener();
        initView();
    }

    private void regisitListener() {

    }

    private void initView() {
        url =   getIntent().getStringExtra(MyContact.URL);
        titlebar_right_view.setVisibility(View.GONE);
        wb_load_hourse_webview = WebViewUtils.getMyInitWebView(wb_load_hourse_webview,this);
        if (NetUtil.isNetworkAvailable(this)) {
            showLoading();
            wb_load_hourse_webview.setWebViewClient(wvc);
            wb_load_hourse_webview.loadUrl(url+ Apis.WEBVIEW_PLETEFORM);//"https://www.baidu.com/"
        }else{//无网的话进行提示
            showToast(getResources().getString(R.string.l_loading_connect_err));
        }
        ViewUtil.setText(HouseHealthyManagerProActivity.this, R.id.titlebar_title_tv, "");
        wb_load_hourse_webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                    ViewUtil.setText(HouseHealthyManagerProActivity.this, R.id.titlebar_title_tv, title);
                }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
        }
    }

    // 创建WebViewClient对象
    WebViewClient wvc = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // 使用自己的WebView组件来响应Url加载事件，而不是使用默认浏览器器加载页面
            wb_load_hourse_webview.loadUrl(url+ Apis.WEBVIEW_PLETEFORM);
            // 记得消耗掉这个事件。给不知道的朋友再解释一下，Android中返回True的意思就是到此为止吧,事件就会不会冒泡传递了，我们称之为消耗掉
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            //Toast.makeText(getApplicationContext(), "WebViewClient.onPageFinished", Toast.LENGTH_SHORT).show();
            super.onPageFinished(view, url);
            hideLoading();
        }


        @Override
        public void onLoadResource(WebView view, String url) {
            //Toast.makeText(getApplicationContext(), "WebViewClient.onLoadResource", Toast.LENGTH_SHORT).show();
            super.onLoadResource(view, url);
        }
        @Override
        public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {
            //Toast.makeText(getApplicationContext(), "WebViewClient.onReceivedError", Toast.LENGTH_SHORT).show();
            super.onReceivedError(view, errorCode, description, failingUrl);
            hideLoading();
        }
        @Override
        public void onScaleChanged(WebView view, float oldScale, float newScale) {
            //Toast.makeText(getApplicationContext(), "WebViewClient.onScaleChanged", Toast.LENGTH_SHORT).show();
            super.onScaleChanged(view, oldScale, newScale);
        }

    };
    /**
     * 设置回退 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
     *  goBack()表示返回webView的上一页面
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return WebViewUtils.onBack(keyCode,event,wb_load_hourse_webview,this);
    }
}
